package modulesAPI.v2.basics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.caterina.components.CLabel;
import com.caterina.components.CPanel;
import com.caterina.resources.CBorders;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;
import com.toedter.calendar.JDateChooser;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;

public class ModCalendar extends Module<Calendar> implements PropertyChangeListener{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di una data
	 * */
	public ModCalendar(Module<?> ownerModule, Calendar content, String tag,String description, ViewType viewType) {
		super(ownerModule, content, tag, description, viewType);
		if (content==null) this.setContent(new GregorianCalendar());
		else this.setContent(content);
		this.calendar=new JDateChooser(this.getContent().getTime());
		calendar.getDateEditor().getUiComponent().setEnabled(false);
		calendar.getDateEditor().getUiComponent().setBorder(CBorders.FIELDS_BORDER);
		this.calendar.addPropertyChangeListener(this);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return CModules.formatPanelForReadOnlyPairs(this.getTag(), CWords.getFormattedDate(this.getContent()));
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return CModules.formatForPairs(this.getTag(), calendar);
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		CPanel retPanel=new CPanel();
		retPanel.add(new CLabel(CWords.getFormattedDate(this.getContent())));
		return retPanel;
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return getReadOnlyPanel(isSelected);
	}
	public int getHierachyNumber(){return -1;}
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equalsIgnoreCase("date")){
			this.setContent(this.calendar.getCalendar());
			this.fireModuleChanged(this);
		}
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {}
	@Override
	protected void afterFireChange(Module<?> mod) {}
	private JDateChooser calendar;
}
